<?php
namespace app\app\controller\v1;

use think\Db;
use think\Request;
use app\app\util\OnlineUtils;

class ClickController extends BaseController{

    /**
     * 显示资源列表
     * 点赞我
     * @return \think\Response
     */
    public function index()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        if(input('get.id') && input('get.id') >0){
            $page = config('PAGE_NUM').input('get.id');
        }else{
            $page = 0;
        }
        $click = Db::query('select a.id,a.user_id,b.username,b.portrait,c.id,c.name,c.position,c.company_name from hmp_click a left join hmp_users b on a.user_id=b.id left join hmp_card c on a.user_id=c.user_id where a.clicked_user_id='.$user_id.' and b.status=1 order by a.id desc limit '.$page.',10');
        if($click){

        }else{
            $click = [];
        }
        return hmp(200,'success',['data'=>$click]);
    }

    /**
     * [getMyClickList 我的点赞]
     * @return [type] [description]
     */
    public function getMyClickList()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        if(input('get.id') && input('get.id') >0){
            $page = config('PAGE_NUM').input('get.id');
        }else{
            $page = 0;
        }
        $click = Db::query('select a.id,a.clicked_user_id as user_id,b.username,b.portrait,c.id,c.name,c.position,c.company_name from hmp_click a left join hmp_users b on a.clicked_user_id=b.id left join hmp_card c on a.card_id=c.id where a.user_id='.$user_id.' and b.status=1 order by a.id desc limit '.$page.',10');
        if($click){

        }else{
            $click = [];
        }
        return hmp(200,'success',['data'=>$click]);        
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     * 点赞
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $id = intval(input('post.id'));
        $card = Db::name('card')->where('id','eq',$id)->find();
        $click = array('user_id'=>$user_id,'clicked_user_id'=>$card['user_id'],'card_id'=>$id);
        $result = Db::name('click')->insertGetId($click);
        if($result){
            Db::query('update hmp_card set click = click + 1 where id ='.$id);
            return hmp(200,'success');
        }else{
            return hmp(402,'fail');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {

    }

    /**
     * 删除指定资源
     * 取消点赞
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {

    }
}